package com.xinhe.web.controller;

import com.querydsl.core.QueryResults;
import com.xinhe.web.annotation.WebUsed;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.Menu;
import com.xinhe.web.response.MenuAuthorityView;
import com.xinhe.web.response.MenuView;
import com.xinhe.web.service.MenuService;
import com.xinhe.web.vo.MenuTreeView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "菜单", tags = {"菜单"})
@RestController
@RequestMapping(value = {"/menu"}, produces = {"application/json;charset=UTF-8"})
public class MenuController {
    @Autowired
    private MenuService menuService;

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "树结构菜单")
    @RequestMapping({"/findTreeMenuByRoleId"})
    public Result findTreeMenuByRoleId(Long roleId) {
        return this.menuService.findTreeMenuByRoleIdIn(new Long[] { roleId });
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "树结构菜单, 仅角色授权调用")
    @RequestMapping({"/findTreeMenuForAuthority"})
    public Result<MenuAuthorityView> findTreeMenuForAuthority(Long roleId) {
        return this.menuService.findTreeMenuForAuthority(roleId);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "查询指定菜单所在树结构")
    @RequestMapping({"/findTreeMenuByMenuId"})
    public Result<List<MenuTreeView>> findTreeMenuByMenuId(Long menuId) {
        return this.menuService.findTreeMenuByMenuId(menuId);
    }
    @WebUsed
    @ApiOperation(httpMethod = "POST", value = "保存菜单")
    @RequestMapping({"/saveMenu"})
    public Result saveMenu(@ApiParam(value = "菜单ID,修改时传")Long id, @ApiParam(value = "设定路由的名字")String routerName, @ApiParam(value = "菜单图标")String icon, @ApiParam(value = "在侧边栏显示的菜单标题")String title,
                           @ApiParam(value = "菜单排序")Integer sequence, @ApiParam(value = "vue组件路径")String component, @ApiParam(value = "类型, 0目录、1菜单、2跳转按钮、3提交按钮")Integer type, @ApiParam(value = "进入路由时携带的参数")String params,
                           @ApiParam(value = "地址(http/https)开头表示外链路由")String path, @ApiParam(value = "父id")Long parentId, @ApiParam(value = "界面终端")Integer terminal, @ApiParam(value = "备注")String remark) {
        return this.menuService.saveModel(id,routerName,icon,title,sequence,component,type,params,path,parentId,terminal,remark);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "删除菜单")
    @ApiImplicitParam(name = "menuId", value = "标识", dataTypeClass = Long.class, required = true)
    @RequestMapping({"/deleteMenu"})
    public Result deleteMenu(Long menuId) {
        return this.menuService.delete(menuId);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "菜单详情")
    @ApiImplicitParam(name = "menuId", value = "标识", dataTypeClass = Long.class, required = true)
    @RequestMapping({"/detailMenu"})
    public Result<Menu> detailMenu(Long menuId) {
        return this.menuService.findById(menuId);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "分页查询")
    @RequestMapping({"/pageMenu"})
    public QueryResults<MenuView> pageMenu(Long parentId,Long roleId,Integer terminal,int pageNum, int pageSize) {
        return this.menuService.pageMenu(parentId,roleId,terminal,pageNum,pageSize);
    }
}